# 1. 设置用户配置, 包括 secretId，secretKey 以及 Region
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from django.conf import settings

import os, django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "saas.settings")
django.setup()

secret_id = settings.TENCENT_COS_SECRET_ID  # 替换为用户的 secretId
secret_key = settings.TENCENT_COS_SECRET_KEY  # 替换为用户的 secretKey
region = 'ap-nanjing'  # 替换为用户的 Region

config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key)

# 2. 获取客户端对象
client = CosS3Client(config)

# 根据文件大小自动选择简单上传或分块上传，分块上传具备断点续传功能。
response = client.upload_file(
    Bucket=settings.TENCENT_BUCKET,
    LocalFilePath='pp.png',
    Key='p1.png',
)

print(response['ETag'])
